/**
 *
 */
package org.jim.connector.command;

import com.huiyun.project.im.consts.ImTenantConst;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jim.core.ImStatus;
import org.jim.core.config.ImConfig;
import org.jim.core.helper.MessageHelper;
import org.jim.core.helper.cs.ICsSessionHelper;
import org.jim.core.packets.cs.session.command.CsSessionReqCmd;
import org.jim.core.packets.cs.session.command.CsSessionRespCmd;
import org.jim.core.packets.cs.session.vo.JimCsCheckSessionReq;
import org.jim.core.packets.cs.session.vo.JimCsCreateSessionReq;
import org.jim.core.packets.cs.session.vo.JimCsCreateSessionResp;
import org.jim.core.packets.cs.session.vo.JimCsSessionConfigResp;
import org.jim.server.config.ImServerConfig;
import org.jim.server.processor.session.CsSessionCmdProcessor;
import org.jim.server.protocol.AbstractProtocolCmdProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author tanyi
 */
@Slf4j
@Component
public class CsSessionCmdProcessorImpl extends AbstractProtocolCmdProcessor implements CsSessionCmdProcessor {
    @Autowired
    private CsAllocHelper csAllocHelper;

    @Override
    public CsSessionRespCmd createCsSession(String userId, CsSessionReqCmd reqBody) {
        CsSessionRespCmd respBody = new CsSessionRespCmd();
        ImServerConfig imServerConfig = ImConfig.Global.get();
        MessageHelper helper = imServerConfig.getMessageHelper();
        ICsSessionHelper sessionService = helper.getCsSessionHelper();
        try {
            JimCsSessionConfigResp jimSessionConfig = sessionService.getConfig(reqBody.getCsTenantId());
            //1.解析session 2.判断是否是当前用户的和当前租户的session  3.检测是否过期,正常返回成功,失败执行后面的逻辑
            JimCsCreateSessionResp jimCreateSessionResp = sessionService.check(new JimCsCheckSessionReq()
                    .setCsTenantId(reqBody.getCsTenantId())
                    .setCsOrgId(reqBody.getCsOrgId())
                    .setUserId(userId)
                    .setSessionId(reqBody.getSessionId())
                    .setAppKey(reqBody.getAppKey())
            );
            if (null != jimCreateSessionResp && StringUtils.isNotBlank(jimCreateSessionResp.getSessionId())) {
                respBody.setCode(ImStatus.C10031.getCode());
                respBody.setData(jimCreateSessionResp);
                return respBody;
            }
            //获取在线客服/创建session
            String csId = csAllocHelper.csAlloc("", reqBody.getCsTenantId(), reqBody.getCsOrgId());
            //临时客服,使用的UUID生成,所以长度较长,作用是无客服在线的时候也能发消息
            if (csId.length() > 20) {
                //页面提示根据需求,是否提示无客服在线
                respBody.setCode(ImStatus.C10031.getCode());
                respBody.setData(new JimCsCreateSessionResp()
                        .setSessionId(TEMP + csId)
                        .setCsTenantCate(ImTenantConst.CATE_4)
                        .setCsTenantId(reqBody.getCsTenantId())
                );
            } else {
                //2.创建session,永远会创建成功,如果客服
                JimCsCreateSessionResp sessionResp = sessionService.create(new JimCsCreateSessionReq()
                        .setCsTenantId(reqBody.getCsTenantId())
                        .setCsId(csId)
                        .setCsOrgId(reqBody.getCsOrgId())
                        .setUserId(userId)
                        .setUserSource(1)
                        .setDevice(1)
                        .setDevicePlatform(1)
                        .setIp("0.0.0.0")
                        .setAppKey(reqBody.getAppKey()));
                //创建客服后的提示
                respBody.setMsg(jimSessionConfig.getSessionMsg());
                respBody.setCode(ImStatus.C10031.getCode());
                respBody.setData(sessionResp);
            }
        } catch (Exception ex) {
            respBody.setCode(ImStatus.C10032.getCode());
            respBody.setMsg(ex.getMessage());
        }
        return respBody;
    }

}
